<!--
  * 分润报表表
  *
  * @Author:    zhoujk
  * @Date:      2025-01-26 06:40:44
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="分润月份" class="smart-query-form-item">
        <a-date-picker picker="month" :allowClear="false" :inputReadOnly="true" v-model:value="queryForm.monthTime" style="width: 200px" />
      </a-form-item>
      <!-- <a-form-item label="支付通道" class="smart-query-form-item">
        <DictSelect style="width: 200px" keyCode="" placeholder="支付通道" v-model:value="queryForm.channelId" width="200px" />
      </a-form-item> -->
      <a-form-item label="状态" class="smart-query-form-item">
        <a-select style="width: 200px" v-model:value="queryForm.confirmedFlag" placeholder="请选择" allowClear>
          <a-select-option :key="1"> 已确认 </a-select-option>
          <a-select-option :key="0"> 待确认 </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ text, record, column }">
        <!-- 有图片预览时 注释解开并把下面的'picture'修改成自己的图片字段名即可 -->
        <!-- <template v-if="column.dataIndex === 'picture'">
            <FilePreview :fileList="text" type="picture" />
          </template> -->

        <!-- 使用字典时 注释解开并把下面的'data-dictionaries'修改成自己的字典字段名即可 有多个字典字段就复制多份同理修改 不然不显示字典 -->
        <!-- 方便修改tag的颜色 orange green purple success processing error default warning -->
        <!-- <template v-if="column.dataIndex === 'data-dictionaries'">
          <a-tag color="cyan">
            {{ text && text.length > 0 ? text.map((e) => e.valueName).join(',') : '暂无' }}
          </a-tag>
        </template> -->

        <template v-if="column.dataIndex === 'confirmedFlag'">
          <a-tag :color="text ? 'success' : 'error'">{{ text ? '已确认' : '待确认' }}</a-tag>
        </template>

        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate" v-if="record.confirmedFlag === false">
            <a-button v-privilege="'profit:report:confirmed'" type="link" size="small" @click="onConfirmed(record)">确认</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <ProfitReportsForm ref="formRef" @reloadList="queryData" />
  </a-card>
</template>
<script setup>
  import dayjs from 'dayjs'
  import { reactive, ref, createVNode, onMounted } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { SmartLoading } from '/src/components/framework/smart-loading'
  import { profitReportApi } from '/@/api/business/b-agent/profit/profit-report/profit-report-api'
  import { PAGE_SIZE_OPTIONS } from '/src/constants/common-const'
  import { smartSentry } from '/src/lib/smart-sentry'
  import TableOperator from '/src/components/support/table-operator/index.vue'
  import DictSelect from '/@/components/business/comment/DataDictionariesSelect.vue'
  import SmartEnumSelect from '/src/components/framework/smart-enum-select/index.vue'
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
  import { formatToMoney } from '/@/utils/common'
  //import FilePreview from '/@/components/support/file-preview/index.vue'; // 图片预览组件

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '分润月份',
      dataIndex: 'monthTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM'),
      width: 90,
      ellipsis: true,
    },
    {
      title: '支付通道',
      dataIndex: 'channelName',
      ellipsis: true,
    },
    {
      title: '交易类型',
      dataIndex: 'payType',
      ellipsis: true,
    },
    {
      title: '交易金额',
      dataIndex: 'tradeAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '交易笔数',
      dataIndex: 'tradeNum',
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'refundAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '退款笔数',
      dataIndex: 'refundNum',
      ellipsis: true,
    },
    {
      title: '营收金额',
      dataIndex: 'receivedAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '结算金额',
      dataIndex: 'settleAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '营收笔数',
      dataIndex: 'receivedNum',
      ellipsis: true,
    },
    {
      title: '分润金额',
      dataIndex: 'profitAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'confirmedFlag',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 90,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    monthTime: undefined, //分润月份
    channelId: undefined, //通道编号
    payType: undefined, //交易类型，0线上，1线下
    pageNo: 1,
    pageSize: 10,
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }
  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      console.log('queryForm===>', queryForm)

      let queryResult = await profitReportApi.page(queryForm)
      // queryResult.data.forEach((e) => {
      //   e.tradeAmt = (e.tradeAmt / 100).toFixed(2)
      //   e.refundAmt = (e.refundAmt / 100).toFixed(2)
      //   e.receivedAmt = (e.receivedAmt / 100).toFixed(2)
      //   e.settleAmt = (e.settleAmt / 100).toFixed(2)
      //   e.profitAmt = (e.profitAmt / 100).toFixed(2)
      // })
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  // 确认
  function onConfirmed(data) {
    Modal.confirm({
      title: '确认提示',
      icon: createVNode(ExclamationCircleOutlined),
      content: createVNode('div', {}, [
        createVNode(
          'div',
          { style: { 'font-size': '18px', 'font-weight': '600' } },
          `${data.channelName}支付通道${data.monthTime}月份佣金金额为${formatToMoney(data.profitAmt / 100)}元`,
        ),
        createVNode('div', { style: { color: '#808080', 'font-size': '14px' } }, '佣金确认操作不可逆，请认真校对相关数据，确保确认后的数据无误！'),
      ]),
      okText: '确认',
      onOk() {
        requestDelete(data)
      },
      cancelText: '取消',
      onCancel() {},
    })
  }

  // 请求确认
  async function requestDelete(data) {
    SmartLoading.show()
    try {
      const params = {
        id: data.id,
      }
      await profitReportApi.confirmed(params)
      message.success('确认完成')
      queryData()
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      SmartLoading.hide()
    }
  }

  onMounted(queryData)
</script>
